#coding=utf-8
from uliweb.orm import *
from uliweb.utils.common import get_var

def get_modified_user():
    from uliweb import request
    if request and request.user:
        return request.user.id
    else:
        return None

class Department(Model):
    __verbose_name__ = u'机构表'
    
    name = Field(str, verbose_name='机构名称', max_length=80, required=True)
    code = Field(str, verbose_name='机构编码', max_length=20, required=True, unique=True, index=True)
    type = Field(CHAR, verbose_name='机构类型', max_length=1, required=True, choices=get_var('PARA/DEPARTMENT_TYPE'))
    kfzx = Reference('kfzx', verbose_name='开发中心')
    delete_flag = Field(bool, verbose_name='删除标志')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    
    class Table:
        fields = [
            {'name':'code', 'width':200},
            'type',
            'kfzx',
            {'name':'name', 'width':400},
            {'name':'action', 'verbose_name':'操作'},
        ]
        
    class EditForm:
        fields = ('kfzx',)
        
    def __unicode__(self):
        return self.name
    
class Team(Model):
    __verbose_name__ = u'团队'
    
    abbr = Field(str, max_length=40, verbose_name="团队简称", required=True, index=True)
    name = Field(str, max_length=255, verbose_name="团队名称", required=True)
    desc = Field(TEXT, verbose_name="团队描述")
    logo = Field(FILE, verbose_name="团队标识")
    kfzx = Reference('kfzx', verbose_name="开发中心")
    
    
    class Table:
        fields = [
                {'name':'abbr', 'width':80},
                {'name':'name', 'width':100},
                {'name':'desc', 'width':300},
                {'name':'kfzx', 'width':100},
                {'name':'logo', 'width':150},
                ]
    
    def __unicode__(self):
        return self.abbr

class Duties(Model):
    __verbose_name__ = u'职务'
    
    name = Field(str, max_length=40, verbose_name="名称", required=True)
    
    def __unicode__(self):
        return self.name
    
    class Table:
        fields = [
                {'name':'id', 'width':40},
                {'name':'name', 'width':200},
                ]
    
    
class Position(Model):
    __verbose_name__ = u'岗位'
    
    name = Field(str, max_length=40, verbose_name="名称", required=True)
    desc = Field(TEXT, verbose_name="描述")
    kfzx = Reference('kfzx', verbose_name="开发中心")
    
    class Table:
        fields = [
                {'name':'name', 'width':100},
                {'name':'desc', 'width':300},
                {'name':'kfzx', 'width':100},
                ]
    
    def __unicode__(self):
        return self.name
 
#from sqlalchemy.dialects.mysql import VARCHAR
class Human(Model):
    __verbose_name__ = u'人员表'
    
    name = Field(str, verbose_name='姓名', max_length=40, required=True)
#    login_name = Field(str, verbose_name='登录名', required=True, max_length=40, unique=True, type_class=VARCHAR, type_attrs=dict(binary=True))
    login_name = Field(str, verbose_name='登录名', required=True, max_length=40, unique=True)
    type = Field(CHAR, verbose_name='类型', max_length=1, choices=get_var('PARA/HUMAN_TYPE'), required=True)
    department = Reference(Department, verbose_name='所属机构', reference_fieldname='code', required=True)
    assigned = Field(bool, verbose_name='是否分配')
    telphone = Field(str, max_length=20, verbose_name='联系电话')
    mobile = Field(str, max_length=16, verbose_name='手机')
    email = Field(str, max_length=30, verbose_name='邮件')
    enter_date = Field(datetime.date, verbose_name='入行时间')
    teams = ManyToMany('team', verbose_name='团队名称', collection_name='team_members')
    duties = Reference('duties', verbose_name='职务名称')
    position = Reference('position', verbose_name='岗位名称')
    delete_flag = Field(bool, verbose_name='删除标志')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    
    class Table:
        fields = [
            {'name':'name', 'width':120},
            {'name':'login_name', 'width':120},
            {'name':'type', 'width':80},
            {'name':'assigned', 'width':80},
            {'name':'workload_total', 'verbose_name':'已分解工作量', 'width':80},                   
            {'name':'teams', 'width':100},
            {'name':'duties', 'width':100},
            {'name':'position', 'width':100},
            {'name':'telphone', 'width':80},            
            {'name':'mobile', 'width':80},            
            {'name':'email', 'width':80},                 
            {'name':'enter_date', 'width':80},                 
            {'name':'department', 'width':300},
        ]
        
    @classmethod
    def OnInit(cls):
        Index('human_indx', cls.c.login_name, cls.c.name, cls.c.department, unique=False)
        
    def __unicode__(self):
        return self.name
    
class Resource(Model):
    __verbose_name__ = u'资源关系表'
    
    user = Reference(Human, verbose_name='登录名', required=True, reference_fieldname='login_name', index=True)
    kfzx = Reference('kfzx', verbose_name='开发中心')
    type = Field(CHAR, verbose_name='类型', max_length=1, choices=get_var('PARA/HUMAN_TYPE'), required=True)
    system = Reference('system', verbose_name='系统', required=True)
    role = Field(int, verbose_name='人员角色', choices=get_var('PARA/RESOURCE_ROLE'))
    aptitude = Field(int, verbose_name='人员资质', choices=get_var('PARA/RESOURCE_APTITUDE'))
    is_selfdev = Field(bool, verbose_name='计入自主研发', choices=get_var('PARA/RESOURCE_APTITUDE'))
    workload = Field(DECIMAL, verbose_name='工作量分解', precision=2, scale=1, required=True, choices=get_var('PARA/WORKLOAD'))
    delete_flag = Field(bool, verbose_name='删除标志')
    delete_date = Field(datetime.date, verbose_name='删除日期')
    
    class EditForm:
        fields = ['system', 'workload', 'role', 'aptitude', 'is_selfdev']
    
    class Table:
        fields = [
            {'name':'name', 'verbose_name':'姓名', 'width':120},
            {'name':'login_name', 'verbose_name':'登录名', 'width':120},
            {'name':'type', 'width':80},
            {'name':'kfzx', 'width':60},
            'system', 'workload', 'role', 'aptitude', 'is_selfdev',
            {'name':'department', 'verbose_name':'所属机构', 'width':300},
            ]
    
    class DetailView:
        fields = ('user', 'kfzx', 'type', {'name':'department', 'label':'所属机构'})
    
    @classmethod
    def OnInit(cls):
        Index('resource_indx', cls.c.kfzx, cls.c.system, cls.c.user, unique=True)
    
class ResourceHis(Model):
    __verbose_name__ = u'资源关系历史表'
    
    yearmon = Field(int, verbose_name="年月")
    user = Reference(Human, verbose_name='登录名', required=True, reference_fieldname='login_name', index=True)
    kfzx = Reference('kfzx', verbose_name='开发中心')
    type = Field(CHAR, verbose_name='类型', max_length=1, choices=get_var('PARA/HUMAN_TYPE'), required=True)
    system = Reference('system', verbose_name='系统')
    role = Field(int, verbose_name='人员角色', choices=get_var('PARA/HUMAN_ROLE'))
    workload = Field(DECIMAL, verbose_name='工作量分解', precision=2, scale=1)
    workday = Field(int, verbose_name='工作天数')
    worktime = Field(DECIMAL, verbose_name='工作时长', precision=5, scale=1)
    delete_flag = Field(bool, verbose_name='删除标志')
    delete_date = Field(datetime.date, verbose_name='删除日期')
    is_selfdev = Field(bool, verbose_name='计入自主研发', choices=get_var('PARA/RESOURCE_APTITUDE'))
    
    @classmethod
    def OnInit(cls):
        Index('resourcehis_indx', cls.c.yearmon, cls.c.kfzx, cls.c.system, cls.c.user, unique=True)
    class Table:
        fields = [
            {'name':'name', 'verbose_name':'姓名', 'width':80},
            {'name':'login_name', 'verbose_name':'登录名', 'width':120},
            {'name':'type', 'width':80},
            {'name':'kfzx', 'width':60},
            'system', 'workload', 'role',
            'workday',
            {'name':'department', 'verbose_name':'所属机构', 'width':100},
            'yearmon',
            ]
    
    
class HumanHis(Model):
    __verbose_name__ = u'人员历史表'
    
    user = Reference('human', verbose_name="登录名", reference_fieldname='login_name', required=True)
    changed_value = Field(BLOB, verbose_name="修改内容")
    old_value = Field(BLOB, verbose_name="原内容")
    modify_flag = Field(CHAR, max_length=1, verbose_name="修改标志", choices=get_var('PARA/AUDIT_MODIFY_FLAG'))
    modified_date = Field(datetime.datetime, verbose_name='修改时间')
    modified_user = Reference('user', verbose_name='修改人', auto=True, auto_add=True)

